{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "\n",
    "import os\n",
    "from datetime import datetime\n",
    "\n",
    "import pandas as pd\n",
    "from tqdm import tqdm\n",
    "\n",
    "from moment.common import PATHS"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "finetuning_mode = 'linear-probing' #'end-to-end'\n",
    "experiment_name = \"supervised_forecasting\" #\"supervised_forecasting/N-BEATS\"\n",
    "dataset_names = ['electricity', 'ETTh1', 'ETTh2', 'ETTm1', 'ETTm2', 'national_illness', 'traffic', 'weather']\n",
    "forecast_horizons = ['96', '720']\n",
    "forecast_horizons_illness = ['24', '60']\n",
    "\n",
    "results_path = os.path.join(PATHS.RESULTS_DIR, experiment_name)\n",
    "print(\"Results path:\", results_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "incomplete_experiments = []\n",
    "results = []\n",
    "\n",
    "for dataset_name in dataset_names:\n",
    "    horizons = None\n",
    "    horizons = forecast_horizons_illness if dataset_name == 'national_illness' else forecast_horizons\n",
    "    \n",
    "    for horizon in horizons:\n",
    "        results_path_complete = os.path.join(\n",
    "            results_path, dataset_name, finetuning_mode, horizon)\n",
    "        \n",
    "        if not os.path.exists(results_path_complete) or len(os.listdir(results_path_complete)) == 0:\n",
    "            incomplete_experiments.append((dataset_name, horizon))\n",
    "            continue\n",
    "        \n",
    "        filenames = os.listdir(results_path_complete)\n",
    "        optim_steps = [f.split('_')[-1][:-4] for f in filenames]\n",
    "        optim_steps = [int(s) for s in optim_steps]\n",
    "        final_optim_step = max(optim_steps)\n",
    "        best_result_path = os.path.join(results_path_complete, f\"results_long-horizon-forecasting_{final_optim_step}.csv\")\n",
    "\n",
    "        df = pd.read_csv(best_result_path, index_col=0).T\n",
    "        df['Dataset Name'] = dataset_name\n",
    "        df['Forecast Horizon'] = horizon\n",
    "        last_modified_time = int(os.path.getmtime(best_result_path))\n",
    "        last_modified_time = datetime.utcfromtimestamp(last_modified_time).strftime('%d-%m-%Y %H:%M')\n",
    "        df['Timestamp'] = last_modified_time\n",
    "\n",
    "        results.append(df)\n",
    "        \n",
    "results = pd.concat(results, axis=0).reset_index(drop=True)\n",
    "results = results[[\n",
    "    'Dataset Name', 'Forecast Horizon', 'MSE', \n",
    "    'MAE', 'MAPE', 'sMAPE', 'RMSE', 'Opt. steps', \n",
    "    'Model name', 'ID', 'Timestamp']]\n",
    "\n",
    "results.to_csv(\"../../assets/results/finetuning/forecasting.csv\", index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(f\"Results: {results.shape}\" )\n",
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import argparse\n",
    "\n",
    "from moment.data.classification_datasets import get_classification_datasets\n",
    "from moment.data.dataloader import get_timeseries_dataloader\n",
    "from moment.utils.config import Config\n",
    "from moment.utils.utils import parse_config"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "config_path = \"../../configs/classification/unsupervised_representation_learning.yaml\"\n",
    "defualt_config_path = \"../../configs/default.yaml\"\n",
    "\n",
    "config = Config(config_file_path=config_path, default_config_file_path=defualt_config_path).parse()\n",
    "args = parse_config(config)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "args.full_file_path_and_name = '/TimeseriesDatasets/classification/UCR/GestureMidAirD2/GestureMidAirD2_TEST.ts'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_dataloader, test_dataloader, val_dataloader =\\\n",
    "              get_dataloaders(args)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
